package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbAddressExample;
import com.dongdongshop.service.AddressService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@DubboService
public class AddressServiceImpl implements AddressService {

    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper tbProvincesMapper;
    @Autowired
    private TbCitiesMapper tbCitiesMapper;
    @Autowired
    private TbAreasMapper tbAreasMapper;

    @Override
    public List<TbAddress> selectAddress(String userId) {
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return tbAddressMapper.selectByExample(example);
    }

    @Override
    public void add(TbAddress tbAddress) {
        tbAddressMapper.insertSelective(tbAddress);
    }

    @Override
    public void deleteInfo(TbAddress tbAddress) {
        tbAddressMapper.deleteByPrimaryKey(tbAddress.getId());
    }

    @Override
    @Transactional
    public List<TbAddress> getAddress(String userId) {
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<TbAddress> addressList = tbAddressMapper.selectByExample(example);
        for (TbAddress tbAddress : addressList) {
            tbAddress.setProvinceId(tbProvincesMapper.selectByPid(tbAddress.getProvinceId()));
            tbAddress.setCityId(tbCitiesMapper.selectNameByCid(tbAddress.getCityId()));
            tbAddress.setTownId(tbAreasMapper.selectNameByAid(tbAddress.getTownId()));
        }
        return addressList;
    }
}
